package com.neusoft.tree.huffman;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.Objects;

/**
 * @author lichunlong lcl253137@alibaba-inc.com
 * @desc 霍夫曼树的节点
 * @date 2025-03-25
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString(exclude = {"leftSubNode", "rightSubNode"})
public class HuffmanNode implements Comparable<HuffmanNode> {

    /**
     * 结点权值
     */
    private int weight;

    /**
     * 左子结点
     */
    private HuffmanNode leftSubNode;

    /**
     * 右子结点
     */
    private HuffmanNode rightSubNode;

    public HuffmanNode(int weight) {
        this.weight = weight;
    }

    /**
     * 前序遍历: 根节点 -> 左子树 -> 右子树
     */
    public void preOrder() {
        System.out.println(this);

        if (Objects.nonNull(this.leftSubNode)) {
            this.leftSubNode.preOrder();
        }
        if (Objects.nonNull(this.rightSubNode)) {
            this.rightSubNode.preOrder();
        }
    }

    @Override
    public int compareTo(HuffmanNode node) {
        // 从小到大排序
        return this.weight - node.weight;
    }
}
